#ifndef MYTCPFACTORY_H
#define MYTCPFACTORY_H

#include <QObject>

class QTcpSocket;

class MyTcpProxy:public QObject
{
    Q_OBJECT
private:
    QTcpSocket* socket;

public:
    MyTcpProxy();
    static MyTcpProxy* getTcpProxy();

    static bool sndLogin(const QString & user,const QString & pwd);
    static bool sndReg(const QString & user,const QString & pwd);
    static bool sndGetDevList(const QString & userid);
    static bool sndDevAdd(const QString& userid, const QString& devid, const QString& devname,
                          const QString& desc);
    static bool sndDevDel(const QString& userid, const QString& devid);
    static bool sndStartCamera(const QString& userid,const QString& devid);
    static bool sndStopCamera(const QString& userid,const QString& devid);
    static bool sndVideoRet(const QString &userid, const QString &devid);

    static int getAvailCnt();

public slots:
    void msgRecved();
    void onDisconn();

signals:
    void loginRet(const bool& ret,const QString& msg);
    void regRet(const bool& ret,const QString& msg);
    void devListRet(const bool&ret, QVector<QVector<QString>> & tableDatas);
    void devAddRet(const bool&ret, const QString& msg);
    void devDelRet(const bool&ret, const QString& msg);
    void startCameraRet(const bool&ret, const QString& msg);
    void stopCameraRet(const bool&ret, const QString& msg);
    void videoRecv(QPixmap& pic);

};


class MsgField{
public:
    unsigned int contentLen;
    QVector<char> contentArr;
public:
    MsgField(unsigned int contentLen,char* contentArr);
    void pushByte(char byte);
};

class MsgPack{
public:
    enum pack_t{
        PACK_LOGIN,PACK_LOGIN_RET,
        PACK_REG,PACK_REG_RET,
        PACK_DEVLIST,PACK_DEVLIST_RET,
        PACK_DEV_ADD,PACK_DEV_ADD_RET,
        PACK_DEV_DEL,PACK_DEV_DEL_RET,
        PACK_START_CAMERA,PACK_START_CAMERA_RET,
        PACK_STOP_CAMERA,PACK_STOP_CAMERA_RET,
        PACK_DEV_HEART,
        PACK_DEV_VEDIO,PACK_DEV_VEDIO_RET
    };

    unsigned int totalLen;
    pack_t type;
    unsigned int filedNum;
    QVector<MsgField> fields;

public:
    MsgPack(pack_t type);
    void addStringField(int contentLen,char* content);
    void addField(int contentLen,char* content);
    MsgField getField(int index);
    QVector<char> pack2Buffer();
    static MsgPack socket2MsgPack(QTcpSocket* socket);

};

#endif // MYTCPFACTORY_H
